package com.aaron.socket.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.aaron.socket.utils.interfaces.PropertiesInter;

/**
 * 
 * @类名称：PropertiesUtils
 * @类描述：编辑propertoes格式的配置文件
 * @创建人：aaron
 * @修改人：
 * @修改时间：2014年5月11日 上午10:48:46
 * @修改备注：
 * @version:1.0.0
 */
public class PropertiesUtils implements PropertiesInter
{

    /**
     * 定义日志类
     */
    private final static Logger logger = Logger.getLogger(PropertiesUtils.class
            .getName());

    /**
     * 配置文件路径
     */
    private static String fileName = "socket_info.properties";

    /**
     * properties对象
     */
    private static Properties properties = new Properties();

    /**
     * 构建一个类实例
     */
    private static PropertiesUtils propertiesUtils = new PropertiesUtils();

    /**
     * 
     * @创建一个新的实例： PropertiesUtils
     */
    private PropertiesUtils()
    {

    }

    /**
     * @方法名：getInstance
     * @参数：@return
     * @返回：PropertiesUtils
     * @exception
     * @since
     */
    public static PropertiesUtils getInstance()
    {
        return propertiesUtils;
    }

    /**
     * 初始化加载
     */
    static
    {

        logger.log(Level.INFO, getMessage("utils.message.log.startLoadFile"));

        InputStream in = null;
        // 构建属性文件路径
        String filePath = ConfigFilePath.getFilePaths() + fileName;

        // 加载属性文件
        try
        {
            in = new BufferedInputStream(new FileInputStream(filePath));

            properties.load(in);
            logger.log(Level.INFO, getMessage("utils.message.log.endLoadFile"));

        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            logger.log(Level.SEVERE,
                    getMessage("utils.message.log.createFileStreamFail"), e);
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            logger.log(Level.SEVERE,
                    getMessage("utils.message.log.loadPropertyFileFail"), e);
        } finally
        {

            try
            {
                in.close();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                logger.log(Level.SEVERE,
                        getMessage("utils.message.log.closeFileStreamFail"), e);
            }
        }
    }

    @Override
    public String readProperty(String key)
    {
        // TODO Auto-generated method stub

        if (null == properties.getProperty(key))
        {
            logger.log(Level.SEVERE,
                    getMessage("utils.message.log.propertyIsNull"));
        }
        return properties.getProperty(key);
    }

    @Override
    public boolean writeProperty(String key, String value)
    {
        // TODO Auto-generated method stub

        return false;
    }

    private static String getMessage(String key)
    {
        return I18MessageUtils.getInstance().getMessage(key);
    }

}
